Micron Document




Java syntax
part 32/46 · 86.7 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
If a class does not specify its superclass, it implicitly inherits from java.lang.Object class. Thus all classes in Java are subclasses of Object class.

If the superclass does not have a constructor without parameters the subclass must specify in its constructors what constructor of the superclass to use. For example:

class Foo {
public Foo(int n) {
// Do something with n
}
}
class Foobar extends Foo {
private int number;
// Superclass does not have constructor without parameters
// so we have to specify what constructor of our superclass to use and how
public Foobar(int number) {
super(number);
this.number = number;
}
}

Overriding methods

Unlike C++, all non-final methods in Java are virtual and can be overridden by the inheriting classes.

class Operation {
public int doSomething() {
return 0;
}
}
class NewOperation extends Operation {
@Override
public int doSomething() {
return 1;
}
}

Abstract classes

An Abstract Class is a class that is incomplete, or is to be considered incomplete, so cannot be instantiated.

A class C has abstract methods if any of the following is true:

• C explicitly contains a declaration of an abstract method.
• Any of C's superclasses has an abstract method and C neither declares nor inherits a method that implements it.
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────